﻿package org.koyabr.webx.demo.danmaku.dal.dataobjs;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.koyabr.webx.demo.danmaku.dal.dao.MulToMulDao;
import org.koyabr.webx.demo.danmaku.dal.dao.PostDao;
import org.koyabr.webx.demo.danmaku.dal.dao.UserDao;
/**
 * 
 * 存放用户的基本信息
 * 以及一些对应关系集合（收藏、喜欢、关注、观看）
 *
 */
public class User {
	/**
	 * 用户名（作为身份唯一标识）
	 */
    private String   username;
    /**
     * 密码
     */
    private String   password;

    /**
     * 个人资料
     */
    private final Profile profile = new Profile(this);
    
    @Autowired
    private MulToMulDao mulDao;
    
    @Autowired
    private UserDao userDao;
    
    @Autowired
    private PostDao postDao;

    public final FollowSet followSet = new FollowSet();
    public final FanSet fanSet = new FanSet();
    public final WatchSet watchSet = new WatchSet();
    public final LikeSet likeSet = new LikeSet();
    public final FavorSet favorSet = new FavorSet();
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    

    public Profile getProfile() {
        return profile;
    }
    
    // 一些关系集合
    
    /**
     * 关注的用户
     */
    public class FollowSet implements MulToMul<User> {
    	public int size(){
    		return toList().size();
    	}
    	
    	public List<User> toList(){
    		
    		LinkedList<User> userList = new LinkedList<User>();
    		
    		for(Map<String, String> map : mulDao.getAllFollow(username)){
    			User user = userDao.getUserByName(map.get("followedName"));
    			if(user != null){
    				userList.add(user);
    			}
    		}
    		
    		return userList;
    	}
    	
    	public boolean contains(User user){
    		return mulDao.getFollow(username, user.getUsername()) != null;
    	}
    	
    	public void add(User user){
    		mulDao.insertFollow(username, user.getUsername());
    	}
    	
    	public void remove(User user){
    		mulDao.deleteFollow(username, user.getUsername());
    	}
    }
    
    /**
     * 被这些用户关注
     */
    public class FanSet implements MulToMul<User> {
    	public int size(){
    		return toList().size();
    	}
    	
    	public List<User> toList(){
    		LinkedList<User> userList = new LinkedList<User>();
    		
    		for(Map<String, String> map : mulDao.getAllFan(username)){
    			User user = userDao.getUserByName(map.get("followingName"));
    			if(user != null){
    				userList.add(user);
    			}
    		}
    		
    		return userList;
    	}
    	
    	public boolean contains(User user){
    		return mulDao.getFollow(user.getUsername(), username) != null;
    	}
    	
    	public void add(User user){
    		mulDao.insertFollow( user.getUsername() ,username);
    	}
    	
    	public void remove(User user){
    		mulDao.deleteFollow( user.getUsername() ,username);
    	}
    }
    
    /**
     * 正在浏览的文章
     */
    public class WatchSet implements MulToMul<Post>{
    	public int size(){
    		return toList().size();
    	}
    	
    	public List<Post> toList(){
    		LinkedList<Post> postList = new LinkedList<Post>();
    		
    		for(Map<String, String> map : mulDao.getAllWatchPost(username)){
    			Post post = postDao.getPostById(map.get("postId"));
    			if(post != null){
    				postList.add(post);
    			}
    		}
    		
    		return postList;
    	}
    	
    	public boolean contains(Post post){
    		return mulDao.getWatch(username, post.getId()) != null;
    	}
    	
    	public void add(Post post){
    		mulDao.insertWatch(username, post.getId());
    	}
    	
    	public void remove(Post post){
    		mulDao.deleteWatch(username, post.getId());
    	}
    }
    /**
     * 喜欢的文章
     */
    public class LikeSet implements MulToMul<Post>{
    	public int size(){
    		return toList().size();
    	}
    	
    	public List<Post> toList(){
    		LinkedList<Post> postList = new LinkedList<Post>();
    		
    		for(Map<String, String> map : mulDao.getAllLikePost(username)){
    			Post post = postDao.getPostById(map.get("postId"));
    			if(post != null){
    				postList.add(post);
    			}
    		}
    		
    		return postList;
    	}
    	
    	public boolean contains(Post post){
    		return mulDao.getLike(username, post.getId()) != null;
    	}
    	
    	public void add(Post post){
    		mulDao.insertLike(username, post.getId());
    	}
    	
    	public void remove(Post post){
    		mulDao.deleteLike(username, post.getId());
    	}
    }
    /**
     * 收藏的文章
     */
    public class FavorSet implements MulToMul<Post>{
    	public int size(){
    		return toList().size();
    	}
    	
    	public List<Post> toList(){
    		LinkedList<Post> postList = new LinkedList<Post>();
    		
    		for(Map<String, String> map : mulDao.getAllFavorPost(username)){
    			Post post = postDao.getPostById(map.get("postId"));
    			if(post != null){
    				postList.add(post);
    			}
    		}
    		
    		return postList;
    	}
    	
    	public boolean contains(Post post){
    		return mulDao.getFavor(username, post.getId()) != null;
    	}
    	
    	public void add(Post post){
    		mulDao.insertFavor(username, post.getId());
    	}
    	
    	public void remove(Post post){
    		mulDao.deleteFavor(username, post.getId());
    	}
    }
    
}